var userModel = require("../models/user.model");
var adminModel = require("../models/administrator.model");
var nodeRSA = require("node-rsa");
var config = require("../config.lib");

module.exports={
    loginVer:function(req, res, next){
        let body = JSON.parse(req.body.data);
        if(body.username && body.password){
            let rsa = new nodeRSA(config.privateKey);//解密
            let password = rsa.decrypt(body.password, "utf8");

            adminModel.find({username: body.username, password: password}, (err, data)=>{
                if(err){
                    console.error(err);
                    res.json({
                        success:false,
                        description:"查询管理员数据库失败"
                    })
                }else{
                    if(data.length > 0){
                        res.json({
                            success:true,
                            data:{
                                data:data,
                                state: 0,//验证成功
                                admin: true,
                            }
                        })
                    }else{
                        userModel.find({username: body.username, password: password}, (err, data)=>{
                            if(err){
                                console.error(err);
                                res.json({
                                    success:true,
                                    description:"查询用户数据库失败"
                                })
                            }else{
                                if(data.length > 0){
                                    res.json({
                                        success:true,
                                        data:{
                                            data:data,
                                            state: 0,//验证成功
                                            admin: false
                                        }
                                    })
                                }else{
                                    res.json({
                                        success:true,
                                        data:{
                                            data: data,
                                            state: 1,//用户名或者密码错误
                                            admin: false
                                        }
                                    })
                                }
                            }
                        })//userModel.find
                    }
                }
            })
            
        }
    }
}